/**
 *卡牌 
 */
package com.mj.scence.game.view
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import resLib.UI;
	
	import utils.core.ObjectPool;
	
	public class MJCard extends Sprite
	{
		public var index:int;//第几格
		
		private var cardValue:int=-1;
		private var bg:Bitmap;
		private var obverseBgBMD:BitmapData;
		private var coverBgBMD:BitmapData;
		private var cardIcon:MovieClip;
		
		public function MJCard($obverseBgBMD:BitmapData, $coverBgBMD:BitmapData)
		{
			super();
			obverseBgBMD = $obverseBgBMD;
			coverBgBMD = $coverBgBMD;
			bg = new Bitmap();
			this.addChild(bg);
			this.mouseChildren = false;
		}
		
		public function get value():int
		{
			return cardValue;
		}
		
		/**
		 *翻转到正面 
		 * @param $cardValue:牌面值
		 * 
		 */		
		public function turnObverse($cardValue:int):void
		{
			bg.bitmapData = obverseBgBMD;
			if(cardIcon == null)
			{
				cardIcon = ObjectPool.getObj("cardIcon".concat($cardValue)) as MovieClip;
				if(cardIcon == null)
				{
					cardIcon = createCardIcon($cardValue);
				}
			}
			else
			{
				if(cardValue != $cardValue)
				{
					ObjectPool.addObj(cardIcon, "cardIcon".concat(cardValue));
					cardIcon = createCardIcon($cardValue);
				}
			}
			cardIcon.x = bg.x + int((bg.width - cardIcon.width)*0.5);
			cardIcon.y = bg.y + int((bg.height - cardIcon.height)*0.5);
			this.addChild(cardIcon);
			cardValue = $cardValue;
			
		}
		
		/**
		 *翻转到背面 
		 * 
		 */		
		public function turnCover():void
		{
			bg.bitmapData = coverBgBMD;
			if(cardIcon && cardIcon.parent)
			{
				cardIcon.parent.removeChild(cardIcon);
				ObjectPool.addObj(cardIcon, "cardIcon".concat(cardValue));
			}
		}
		
		
		/**
		 *生成一张牌值图标 
		 * @param v
		 * @return 
		 * 
		 */		
		private function createCardIcon(v:int):MovieClip
		{
			var icon:MovieClip;
			
			icon.mouseChildren = false;
			icon.mouseEnabled = false;
			icon.gotoAndStop(1);
			return icon;
			
		}
	}
}